package com.ibook.controller;

import com.ibook.common.Const;
import com.ibook.common.ServerResponse;
import com.ibook.pojo.Manager;
import com.ibook.service.ManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * Created by heziqi on 2017/6/9.
 */
@Controller
@RequestMapping("/manager/")
public class ManagerController {

    @Autowired
    private ManagerService managerService;

    /**
     * 管理员登录
     * @param managerName
     * @param password
     * @param httpSession
     * @return
     */
    @RequestMapping(value = "login.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<Manager> login(String managerName, String password, HttpSession httpSession){
        ServerResponse<Manager> response = managerService.login(managerName, password);
        if (response.isSuccess()){
            httpSession.setAttribute(Const.CURRENT_MANAGER,response.getData());
        }
        return response;
    }

    /**
     * 管理员退出登录
     * @param httpSession
     * @return
     */
    @RequestMapping(value = "logout.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse logout(HttpSession httpSession){
        httpSession.removeAttribute(Const.CURRENT_MANAGER);
        return ServerResponse.createBySuccessMessage("退出登录成功");
    }

}
